#ifndef THREADS_SYNCH_H
#define THREADS_SYNCH_H

#include <list.h>
#include <stdbool.h>

/* A counting semaphore. */
struct semaphore 
  {
    unsigned value;             /* Current value. */
    struct list waiters;        /* List of waiting threads. */
  };

void sema_init (struct semaphore *, unsigned value);
void sema_down (struct semaphore *);
bool sema_try_down (struct semaphore *);
void sema_up (struct semaphore *);
void sema_self_test (void);

// 3 idiots added begin
// struct for priority-donation control list
struct donation_record_node
{
  struct thread *donated_thread;	/* donate this thread (lock-holder)*/
  struct list_elem elem;		/* list element */
  int donated_priority;			/* donated priority */
                                	/* if this value is -1 */
					/* it means it was set to be lower priority than it had */
					/* when it had donated priority */
  int original_priority;		/* priority before donated */
					/* priority that it was set to be lower */
					/* when it had donated priority */
};
// 3 idiots added end

/* Lock. */
struct lock 
  {
    struct thread *holder;      /* Thread holding lock (for debugging). */
    struct semaphore semaphore; /* Binary semaphore controlling access. */
    
    // 3 idiots added begin
    struct list donated_thread_list;	/* lock has its own list about thread that is donated */
    // 3 idiots added end
  };

// 3 idiots added begin
bool donated_priority_more_func(const struct list_elem *left, const struct list_elem *right, void *aux);
bool semaphore_priority_more_func (const struct list_elem *left, const struct list_elem *right, void *aux);
// 3 idiots added end

void lock_init (struct lock *);
void lock_acquire (struct lock *);
bool lock_try_acquire (struct lock *);
void lock_release (struct lock *);
bool lock_held_by_current_thread (const struct lock *);

/* A condition variable. */
struct condition 
  {
    struct list waiters;        /* List of waiting threads. */
  };

void cond_init (struct condition *);
void cond_wait (struct condition *, struct lock *);
void cond_signal (struct condition *, struct lock *);
void cond_broadcast (struct condition *, struct lock *);

/* Optimization barrier.

   The compiler will not reorder operations across an
   optimization barrier.  See "Optimization Barriers" in the
   reference guide for more information.*/
#define barrier() asm volatile ("" : : : "memory")

#endif /* threads/synch.h */

